package com.service.oos;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import com.common.spring.SpringMvcUtils; 
import com.dao.oos.OosDataDao;
import com.model.oos.OosDataModel;
import com.model.sys.SysUser;
import com.service.sys.WXService;

import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.dao.oos.OosImprovesDao;
import com.model.oos.OosImprovesModel;
import com.common.model.Global;

@Service
public class OosImprovesService {
    static final Logger logger = LoggerFactory.getLogger(OosImprovesService.class);

    @Autowired
    OosImprovesDao oosImprovesDao;
    @Autowired
    OosDataDao oosDataDao;
	@Autowired
	WXService wxService;
    /**
     * 列表查询
     *
     * @param queryMap
     * @return
     */
    public HttpResult selectList(Map<String, Object> queryMap) {
        HttpResult httpResult = new HttpResult();
        EasyuiUtils.addPageNum(queryMap);
        EasyuiUtils.addFilter(queryMap);
        List<OosImprovesModel> list = oosImprovesDao.selectList(queryMap);
        int count = oosImprovesDao.selectListCount(queryMap);
        httpResult.setRows(list);
        httpResult.setTotal(count);
        logger.info("selectList:{}", list);
        return httpResult;
    }

    /**
     * 退回
     *
     * @param impId
     * @return
     */
    public int backUp(String impId) throws IOException { 
        int result = 0;
        OosImprovesModel oosImprovesModel = oosImprovesDao.load(impId);
        if (null != oosImprovesModel) {
            OosDataModel oosDataModel = oosDataDao.load(oosImprovesModel.getOsId());
            result = oosImprovesDao.backUp(impId);
            if (result >0) {
            	wxService.sendTxtMessage(oosImprovesModel.getImpApprovelCode(), "【OOS提醒消息】您好，编号："+oosDataModel.getOsCode()+"的OOS项目中，您的行动项被退回，请登录CMP查看。");
            }
        }
        return result;
    }

    /**
     * 列表查询
     *
     * @param queryMap
     * @return
     */
    public HttpResult selectListDutyDept(Map<String, Object> queryMap) {
        HttpResult httpResult = new HttpResult();
        /*EasyuiUtils.addPageNum(queryMap);
        EasyuiUtils.addFilter(queryMap);*/
        // 只查询属于当前登录人部门的task
        SysUser sysUser = SpringMvcUtils.getSysUser();
        if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
            if (StringUtils.isNotNull(sysUser.getDatapermission())) {
                String[] datapermission = sysUser.getDatapermission().split(",");
                if (ArrayUtils.contains(datapermission, sysUser.getDeptid())) {
                    queryMap.put("impDutyDeptCode", sysUser.getDeptid());
                } else {
                    // 查询当前登录人为负责人的imporve
                    queryMap.put("impDutyPersonCode", sysUser.getLoginname());
                }
            } else {
                // 查询当前登录人为负责人的imporve
                queryMap.put("impDutyPersonCode", sysUser.getLoginname());
            }
        }
        List<OosImprovesModel> list = oosImprovesDao.selectListByOsId(queryMap);
       /* int count = oosImprovesDao.selectListCount(queryMap);
        httpResult.setTotal(count);*/
        httpResult.setData(list);

        logger.info("selectList:{}", list);
        return httpResult;
    }

    /**
     * 此方法供各OOS任务责任部门使用，部门负责人可看到所有与本部门相关的OOS行动项与OOS整改措施
     *
     * @param queryMap
     * @return
     */
    public HttpResult selectDepartmentalList(Map<String, Object> queryMap) {
        HttpResult httpResult = new HttpResult();
        EasyuiUtils.addPageNum(queryMap);
        EasyuiUtils.addFilter(queryMap);
        SysUser sysUser = SpringMvcUtils.getSysUser();
        // 管理员可以查看全部的数据
        if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
            // 只查询属于当前登录人部门的整改项
            if (StringUtils.isNotNull(sysUser.getDatapermission())) {
                String[] datapermission = sysUser.getDatapermission().split(",");
                if (ArrayUtils.contains(datapermission, sysUser.getDeptid())) {
                    queryMap.put("impDutyDeptCode", sysUser.getDeptid());
                } else {
                    // 不具有部门权限的，只能查看与本人相关的数据
                    queryMap.put("impDutyPersonCode", sysUser.getLoginname());
                }
            } else {
                // 不具有部门权限的，只能查看与本人相关的数据
                queryMap.put("impDutyPersonCode", sysUser.getLoginname());
            }
        }
        List<OosDataModel> list = oosImprovesDao.selectDepartmentalList(queryMap);
        int count = oosImprovesDao.selectDepartmentalListCount(queryMap);
        httpResult.setRows(list);
        httpResult.setTotal(count);
        logger.info("selectDepartmentalImprovesList:{}", list);
        return httpResult;

    }

    /**
     * 详情
     *
     * @param
     * @return
     */
    public HttpResult selectInfor(String impId) {
        HttpResult httpResult = new HttpResult();
        OosImprovesModel oosImprovesModel = oosImprovesDao.load(impId);
        httpResult.setData(oosImprovesModel);
        httpResult.setCode(HttpStatus.SUCCESS_CODE);
        return httpResult;
    }

    /**
     * 保存
     *
     * @param
     * @return
     */
    public HttpResult save(OosImprovesModel oosImprovesModel) {
        if (oosImprovesModel == null) {
            return HttpResult.error("参数不能为空");
        }
        HttpResult httpResult = new HttpResult();
        int result = 0;
        if (!StringUtils.isNotNull(oosImprovesModel.getImpId())) {
            oosImprovesModel.setImpId(UUID.randomUUID().toString());
            result = oosImprovesDao.insert(oosImprovesModel);
        } else {
            result = oosImprovesDao.update(oosImprovesModel);
        }
        if (result > 0) {
            httpResult.setMsg(oosImprovesModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
            httpResult.setData(oosImprovesModel);
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg(oosImprovesModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
        }
        return httpResult;
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    public HttpResult delete(String impId) {
        HttpResult httpResult = new HttpResult();
        if (!StringUtils.isNotNull(impId)) {
            return HttpResult.error("参数不能为空");
        }
        int result = oosImprovesDao.delete(impId);
        if (result > 0) {
            httpResult.setMsg("删除成功！");
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg("不存在此条数据！");
        }
        return httpResult;
    }

    /**
     * 导出
     *
     * @param queryMap
     * @param response
     */
    public void export(Map<String, Object> queryMap, HttpServletResponse response) {
        List<OosImprovesModel> list = oosImprovesDao.selectList(queryMap);
        logger.info("export:{}", list);
        try {
            EasyExcel.write(response.getOutputStream(), OosImprovesModel.class).sheet("模板").doWrite(list);
        } catch (IOException e) {
            logger.error("导出错误:{}", e.getMessage());
        }

    }
}
